{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 序列化： 使用文件来管理提示词模板"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "9be1bfbaff6a3ce3"
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "from langchain.prompts import load_prompt"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-06T14:21:36.378669700Z",
     "start_time": "2024-05-06T14:21:34.843779400Z"
    }
   },
   "id": "de6a02aff1978f89"
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "给我讲一个关于小黑的恐怖的故事\n"
     ]
    }
   ],
   "source": [
    "# 加载yaml格式的prompt模板\n",
    "prompt = load_prompt(\"simple_prompt.json\")\n",
    "print(prompt.format(name = \"小黑\", what=\"恐怖的\"))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-06T14:25:10.258656600Z",
     "start_time": "2024-05-06T14:25:10.249657100Z"
    }
   },
   "id": "c29360acfb928ac8"
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "a164a069f32e579"
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "{'answer': 'George Washington was born in 1732 and died in 1799.',\n 'score': '1/2'}"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 支持加载文件格式的模板，并且对prompt的最终解析结果进行自定义格式化\n",
    "prompt = load_prompt(\"prompt_with_output_parser.json\")\n",
    "prompt.output_parser.parse(\n",
    "    \"George Washington was born in 1732 and died in 1799.\\nScore: 1/2\"\n",
    ")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-06T14:35:18.297799700Z",
     "start_time": "2024-05-06T14:35:18.290761500Z"
    }
   },
   "id": "32bf83e7221f17dd"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "6e0ef366be6c3fa8"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
